package messageprocessors.clients;

import java.util.Map;

import org.apache.log4j.Logger;


import messageprocessors.MessageProcessor;
import messages.DSMessage;
import messages.DSMessageType;


public abstract class BaseClient extends MessageProcessor {	
	
	private static Logger log = Logger.getLogger(BaseClient.class);
	
	public BaseClient(String id, String xmppPassword) {
		super(id, xmppPassword);
	}
	
	/**
	 * executes super.connect()
	 * sends a status message to the server
	 */
	@Override
	public void connect() throws Exception {
		super.connect();
		reportStatus(javaServerId);
	}

	/**
	 * sends a full status message to the server
	 * fills the targetid member variable of the message with the given targetadress string
	 * @throws Exception
	 */
	public void reportStatus(String targetadress) throws Exception {
		log.debug("Reporting status from: " + this.getId() + ", to: " + targetadress);
		Map<String,String> propMap = generatePropertyMap();
		DSMessage msg = new DSMessage(propMap);
		msg.setType(DSMessageType.STATUS);
		msg.setOriginId(getId());
		msg.setTargetId(targetadress);
		
		sendMessage(msg, javaServerId);
	}
	
	/**
	 * prints the current status values to console
	 */
	public void printStatusToConsole() {
		System.out.println(getId() + " status: " + generatePropertyMap());
	}
	
	/**
	 * generates a hashmap with all available property names as keys and their actual values 
	 * @return
	 */
	protected abstract Map<String,String> generatePropertyMap();
	
	protected String convertBoolToString(boolean value) {
		if(value) {
			return "true";
		} else {
			return "false";
		}
	}
	
	protected String convertIntToString(int val) {
		String str = "";
		str+= val;
		return str;
	}

}
